package com.ctbu.yyx.cameracontrol.webapi;

import com.ctbu.yyx.cameracontrol.domain.User;
import com.ctbu.yyx.cameracontrol.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi")
public class LoginRestController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public int loginuser (HttpServletRequest httpreqeust, String username, String userpassword)throws Exception{

        List<User> users = userService.findByUserNameAndUserPassword(username,userpassword);
        if(users.size()>0){
            HttpSession session = httpreqeust.getSession();
            session.setAttribute("userid", users.get(0).getId());
            session.setAttribute("user", users.get(0));
            return users.get(0).getId();
        }else {
            return -1;
        }

    }
}
